package com.edu.dao.impl;

import com.edu.bean.Admin;
import com.edu.dao.AdminDao;
import com.edu.dao.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author:xgdxqr
 * @Date:2025/9/19-13:22-22
 **/

public class AdminDaoImpl implements AdminDao {
    @Override
    public Admin selectAdmin(String account, String password) {
        Admin admin = null;
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet res = null;
        String sql = "SELECT * FROM admin WHERE account=? AND password=?";
        try {
            con = BaseDao.getCon();
            ps = con.prepareStatement(sql);
            ps.setString(1,account);
            ps.setString(2,password);
            res = ps.executeQuery();
            if(res.next()){
                admin = new Admin();
                admin.setAdminId(res.getInt("AdminId"));
                admin.setAccount(res.getString("Account"));
                admin.setPassword(res.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admin;
    }

    @Override
    public Admin selectAdminByAccount(String account) {
        Admin admin = null;
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet res = null;
        String sql = "SELECT * FROM admin WHERE account=?";
        try {
            con = BaseDao.getCon();
            ps = con.prepareStatement(sql);
            ps.setString(1,account);
            res = ps.executeQuery();
            if(res.next()){
                admin = new Admin();
                admin.setAdminId(res.getInt("AdminId"));
                admin.setAccount(res.getString("Account"));
                admin.setPassword(res.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admin;
    }
}
